<?php
/**
* 2017年5月22日 14:04:22
* author：silenceluo
* emai：1095584081@qq.com
* HTTP请求类
*/
class Request
{
	
	/**
	 * http get请求
	 * @param  [type]  $url    [请求地址]
	 * @param  integer $header [http请求头]
	 * @return [type]          [description]
	 */
	function _get($url, $header = 0)
	{
		//初始化一个curl会话
		$curl = curl_init();
		//设置请求地址
		curl_setopt($curl, CURLOPT_URL, $url);
		//获取返回值
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置代理 本地不设置会报错
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//设置http请求头
		if(is_array($header))
		{
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		//运行curl
		$result = curl_exec($curl);
		//资源关闭
		curl_close($curl);

		return json_decode($result,true);
	}
	/**
	 * http post请求
	 * @param  [type]  $url    [description]
	 * @param  [type]  $data   [description]
	 * @param  integer $header [description]
	 * @return [type]          [description]
	 */
	function _post($url, $data, $header = 0)
	{
		//初始化curl会话
		$curl = curl_init();
		//设置请求地址
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置返回
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置post请求
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		//设置代理 本地不设置会报错
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//设置http请求头
		if(is_array($header)){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		//运行curl
		$result = curl_exec($curl);
		//资源关闭
		curl_close($curl);
		
		return json_decode($result,true);

	}
	/**
     * @abstract xml数据发送
     */
	function xmlCurlPost($xmlData, $url, $timeoutMs=30000)
	{
		$ch = curl_init();
		$header[] = "Content-type: text/xml";//定义content-type为xml
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
		curl_setopt($ch, CURLOPT_NOSIGNAL, 1);    //注意，毫秒超时一定要设置这个
		curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeoutMs);  //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
		$sContent = curl_exec($ch);
		$aStatus = curl_getinfo($ch);
		curl_close($ch);
		if (intval($aStatus["http_code"])==200)
		{
		    return trim($sContent);
		}
		else
		{
		    return false;
		}
	}
}